package com.yuyou.tas.report.controller;

import com.yuyou.tas.report.service.IExamModelReportService;
import com.yuyou.tas.report.vo.resp.IsExistResp;
import com.yuyou.tas.report.vo.resp.RecordDataVo;
import com.yuyou.tas.util.bean.LoginRedisObj;
import com.yuyou.tas.util.bean.ReturnResult;
import com.yuyou.tas.util.exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

@Api(value="ExamScoreController",description="试卷模板录分接口")
@Validated
@RestController
@RequestMapping(value = "/api/examScore/")
public class ExamScoreController extends BaseController{

    @Autowired
    private IExamModelReportService examModelReportService;

    @ApiOperation(value = "录分页数据")
    @RequestMapping(value = "/getStudents", method = { RequestMethod.GET,RequestMethod.POST })
    public ReturnResult<RecordDataVo> getStudents(@ApiParam(value = "班级ID")@RequestParam(value = "classId")@Validated @NotNull(message = "班级ID不能为空") Long classId,
                                                           @ApiParam(value = "模板ID")@RequestParam(value = "modelId")@Validated @NotNull(message = "模板ID不能为空") Long modelId,
                                                                  HttpServletRequest request) throws Exception {
        LoginRedisObj loginObj = this.getUserObj(request);
        RecordDataVo recordDataVo  = examModelReportService.getStudents(classId , modelId ,loginObj.getOrganizationId());
        return ReturnResult.success(recordDataVo);
    }

    @ApiOperation(value = "录分")
    @RequestMapping(value = "/record", method = { RequestMethod.GET,RequestMethod.POST })
    public ReturnResult<Void> record(@Validated @ApiParam(value = "学生ID")@RequestParam(value = "stuId")@NotNull(message = "学生ID不能为空") Long stuId,
                                     @ApiParam(value = "模板ID")@RequestParam(value = "modelId")@NotNull(message = "模板ID不能为空") Long modelId,
                                     @ApiParam(value = "班级ID")@RequestParam(value = "classId") @NotNull(message = "班级ID不能为空") Long classId,
                                     @ApiParam(value = "考试状态:0:参考，1:缺考")@RequestParam(value = "status") @NotNull(message = "状态不能为空") Integer status,
                                     @ApiParam(value = "分数列表")@RequestParam(value = "scores" , required = false)String[] scores,
                                                           HttpServletRequest request){
        LoginRedisObj loginObj = this.getUserObj(request);
        if(status == 0){//参考
            if(scores == null || scores.length < 1){
                throw new BusinessException("分数不能为空");
            }
            examModelReportService.record(classId ,stuId , modelId , scores , loginObj.getOrganizationId());
        }else{//缺考
            examModelReportService.recordMiss(classId , stuId , modelId ,loginObj.getOrganizationId());
        }
        return ReturnResult.success("OK");
    }

    @ApiOperation(value = "该学生在班级内同一个活动是否存在录分数据")
    @RequestMapping(value = "/isExist", method = { RequestMethod.GET,RequestMethod.POST })
    public ReturnResult<IsExistResp> isExist(@Validated @ApiParam(value = "学生ID")@RequestParam(value = "stuId")@NotNull(message = "学生ID不能为空") Long stuId,
                                     @ApiParam(value = "模板ID")@RequestParam(value = "modelId")@NotNull(message = "模板ID不能为空") Long modelId,
                                     @ApiParam(value = "班级ID")@RequestParam(value = "classId") @NotNull(message = "班级ID不能为空") Long classId){

        IsExistResp existResp = examModelReportService.isExist(stuId , modelId , classId);
        return ReturnResult.success(existResp);
    }
}
